/**
 * 
 */
package annotool.clustering;

import java.util.ArrayList;
import java.util.Vector;

import mpicbg.imagefeatures.Feature;

/**
 * @author DIVAKARUNI
 *
 */
public class Histogram 
{

	/**
	 * 
	 */
	public int featureSize;
	float[][] features;
	int length = 0;
	int dimension = 0;
	int maxClass;
	float[][] testingFeatures,  selectedTrainingFeatures;
	float[][][] testingfeaturesperimage, codebook;
	int[][] codebook_freq;


	public Histogram(float[][] testingFeatures, float[][] selectedTrainingFeatures, int featureSize) 
	{
		this.featureSize = featureSize;
		this.testingFeatures = testingFeatures;
		this.selectedTrainingFeatures = selectedTrainingFeatures;
	}

	public float[][] generator() 
	{
		features = new float[testingFeatures.length][];
		testingfeaturesperimage = getFeaturesPerImage(testingFeatures);
		codebook = getFeaturesPerImage(selectedTrainingFeatures);
		codebook_freq = new int[testingFeatures.length][];
		//dec 09
		
		Vector<Feature> fs1, fs2 = new Vector<Feature>();
		for(int j = 0; j < codebook.length; j++)
		{
			for(int perfeat = 0; perfeat < codebook[j].length; perfeat++)
				fs2.add(new Feature(codebook[j][perfeat]));
		}
		
		
		for(int i = 0; i < testingfeaturesperimage.length; i++)
		{
			codebook_freq[i] = new int[fs2.size()];
			for(int j = 0; j < fs2.size(); j++)
				{codebook_freq[i][j] = 0;

				}
				fs1 = new Vector<Feature>();
			for(int perfeat = 0; perfeat < testingfeaturesperimage[i].length; perfeat++)
				{fs1.add(new Feature(testingfeaturesperimage[i][perfeat]));}
			ArrayList<Float> ratio = new ArrayList<Float>(); 

			//for(int j = 0; j < codebook.length; j++)
			//{
				float[][] featurefloats2d = new float[testingfeaturesperimage.length][featureSize];
				for ( Feature original : fs1)
				{
					float d = 0;
					Feature f1 = original;
					float best_d = Float.MAX_VALUE;
					float second_best_d = Float.MAX_VALUE;
					int f2count = 0;
					int bestf2 = -1;
					for ( Feature f2 : fs2 )
					{
						d = f1.descriptorDistance( f2 );
						if ( d < best_d )
						{
							second_best_d = best_d;
							best_d = d;
							bestf2 = f2count;
						}
						else if ( d < second_best_d )
							second_best_d = d;
						f2count++;
					}
					if(bestf2 != -1)
						{codebook_freq[i][bestf2]++;
						}
						
					
				}

			//}
			features[i] = new float[codebook_freq[i].length];
			//System.out.println("rep: "+ratio.size());
			for(int r = 0; r < codebook_freq[i].length; r++)
			{
				features[i][r] = codebook_freq[i][r];
				//System.out.print(" "+(r+1)+":"+features[i][r]);
				//System.out.print(" "+features[i][r]);

			}
			//System.out.println();
		}
		return features;
	}

	float[][][] getFeaturesPerImage(float[][] getfeatures)
	{
		float[][][] featuresperimage;

		featuresperimage = new float[getfeatures.length][][];

		for(int i = 0;i < getfeatures.length; i++)
		{
			featuresperimage[i] = new float[(getfeatures[i].length)/featureSize][featureSize];

			for(int j = 0;j < (getfeatures[i].length)/featureSize; j++)
			{
				for(int k = 0;k < featureSize;k++)
				{
					featuresperimage[i][j][k] = getfeatures[i][(j * featureSize) + k];
				}

			}

		}
		return featuresperimage;
	}

}
